package org.web.controller;

import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.web.domain.Classify;
import org.web.domain.Info;
import org.web.domain.XinWen;
import org.web.exception.UnActiveException;
import org.web.service.WenZhangService;
import sun.security.util.AuthResources_it;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.rmi.activation.ActivationException;
import java.util.List;

/**
 * @Author:芯古子
 * @Date:2022/4/17 15:17
 **/
@Controller
public class WenZhang {
    @Resource(name = "wenZhangService")
    private WenZhangService service;
    @ResponseBody
    @RequestMapping("all")
    public List<XinWen> list(){
        List<XinWen> xin = service.xin();
        return xin;
    }
    @RequestMapping("delete")
    public String delete(String title){
        service.delete(title);
        return "redirect:list.html";
    }
    @ResponseBody
    @RequestMapping("add12")
    public Info add(HttpServletRequest request){
        Info info = new Info(true);
        try {
            String title = request.getParameter("title");
            String matter = request.getParameter("matter");
            String classtype = request.getParameter("classtype");
            String time = request.getParameter("time");
            if(title.equals("")){
                info.setData(false);
                throw new UnActiveException("标题不能为空,上传失败");
            }
            if(matter.equals("")){
                info.setData(false);
                throw new ActivationException("内容不能为空,上传失败");
            }
            XinWen xinWen = new XinWen();
            if(classtype.equals("疫情")){
                xinWen.setClassid(1);
            } if(classtype.equals("生活")){
                xinWen.setClassid(2);
            } if(classtype.equals("穿搭")){
                xinWen.setClassid(3);
            } if(classtype.equals("美甲")){
                xinWen.setClassid(4);
            }
            xinWen.setMatter(matter);
            xinWen.setTitle(title);
            xinWen.setTime(time);
            service.add2(xinWen);
            info.setData(true);
        } catch (Exception e) {
            info.setData(false);
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }
        return info;
    }

    @ResponseBody
    @RequestMapping("xiu")
    public String update2(XinWen xinWen,HttpServletRequest request,String title){
        service.upa2(title);
        String classtype = request.getParameter("classtype");
        if(classtype.equals("疫情")){
            xinWen.setClassid(1);
        } if(classtype.equals("生活")){
            xinWen.setClassid(2);
        } if(classtype.equals("穿搭")){
            xinWen.setClassid(3);
        } if(classtype.equals("美甲")){
            xinWen.setClassid(4);
        }
        service.up2(xinWen);
        return "redirect:list.html";
    }
    @ResponseBody
    @RequestMapping("key")
    public List<XinWen> key(HttpServletRequest request,Integer id){
        String keyword = request.getParameter("keyword");
        List<XinWen> xinWens = service.key2(keyword,id,keyword);
        return xinWens;
    }
//查找标签
    @ResponseBody
    @RequestMapping("/selectClassName")
    public List<Classify> selectClassName(){
        List<Classify> classify = service.selectClassName();
        return classify;
    }
//阅读文章，阅读数加1
    @RequestMapping("/watch")
    public void  watch(HttpServletRequest request){
        String like = request.getParameter("like");
        int i = Integer.parseInt(like);
        service.watch(i);
    }
    @ResponseBody
    @RequestMapping("/sup")
    public String su(HttpServletRequest request){
        String support = request.getParameter("support");
        int i = Integer.parseInt(support);
        service.support(i);
        return "redirect:list.html";
    }
    @RequestMapping("/sup2")
    public String su2(HttpServletRequest request){
        String support = request.getParameter("support");
        int i = Integer.parseInt(support);
        service.support2(i);
        return "redirect:list.html";
    }
    @ResponseBody
    @RequestMapping("/shang" )
    public XinWen shang(HttpServletRequest request){
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        if(i==1){
            i=2;
        }
        XinWen shang = service.shang(i);
        System.out.println(shang);
        return shang;
    }
     @ResponseBody
    @RequestMapping("/xia" )
    public XinWen xia(HttpServletRequest request){
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        if(i==6){
            i=5;
        }
        XinWen shang = service.xia(i);
        System.out.println(shang);
        return shang;
    }

}
